//引入绘图方法
let canvas = require('../../common/js/canvas.js');
let Axios = require('../../common/js/ajax.js');
let Mtils = require('../../common/js/Mtils.min.js');
Mtils = Mtils.Mtils
var page = 1;
var switch_p = true
//获取应用实例
const app = getApp()
Page({
	data: {
		height: 866,
		top: 106,
		imgHeight: 0,
		hcImg: '',
		indicatorDots: false, //swiper属性
		duration: 500, //swiper属性
		autoplay:true,
		interval:2000,
		openid: '',
		nickName: '',
		avatarUrl: '',
		curentNav: 1, //当前导航
		curr_id: '', //当前视频显示id
		vpause: false, //视频加载完成的判断
		curTk_id: '', //当前分享弹窗id		
		saleing: false, //正在售卖
		myfollow: false, //我的关注
		signCls: false, //产品分类
		tk: false,
		followTk: false, //取消关注
		follow_id: '',
		follow_index: '',
		canvasTk: false, //合成图片
		repTk: false, //举报
		rep_id: '',
		rep_index: '',
		repTkCon: '',
		cur_key: '',
		cur_keywords: '',
		firstKey: 0,
		tips: false,
		signList: [],
		indexList: [],
		banners: [],
		showImg:[]
	},
	//事件处理函数
	onLoad: function() {
		this.load = this.selectComponent("#loading");
		this.login = this.selectComponent("#login");
		var _this = this;	
		this.uid = app.globalData.uid || ''
		this.Md5 = Mtils.security.hex_md5(this.uid) + app.globalData.secret;
		this.Md5 = Mtils.security.hex_md5(this.Md5);
		this.timesp = Mtils.now();
		if (app.globalData.nickName && app.globalData.avatarUrl) {
			this.setData({
				nickName: app.globalData.nickName,
				avatarUrl: app.globalData.avatarUrl
			})
		}
		Axios.get(app.globalData.baseUrl + 'App/PublicCommon/Article/z_banner/h_banner', { //banner
			p: 1
		}, (res) => {
			if (res.status == 1) {
				var data = res.info
				_this.setData({
					banners: data
				})
			}
		})
		Axios.get(app.globalData.baseUrl + 'App/PublicCommon/Goods/get_goods_hotwords', { //关键字
			p: 1
		}, (res) => {
			if (res.status == 1) {
				var data = res.info
				_this.setData({
					signList: data
				})
			}
		})
	},
	onShow(){
		var _this = this;
		Axios.get(app.globalData.baseUrl + 'App/PublicCommon/Goods/search_goods', {
			uid: this.uid
		}, (res) => {
			console.log(res)
			if (res.status == 1) {
				var data = res.info
				_this.setData({
					indexList: data,
				})
				_this.data.indexList.forEach(function(item) {
					item.toggle = false;
				})
				_this.load.loadIng()
			}
		})
	},
	onReady: function() { //增加toggle 属性
		wx.setNavigationBarTitle({
			title: '甄藏汇'
		})
	},
	preventTouchMove: function() {},
	bubbling: function() {},
	showModal: function(error) {
		wx.showModal({
			content: error.msg,
			showCancel: false,
		})
	},
	onReachBottom: function() {
		var _this = this
		var is_follow = 0;
		if (this.data.myfollow == true) {
			is_follow = 1
		} else {
			is_follow = 0
		}
		var is_on_sale = 0;
		if (this.data.saleing == true) {
			is_on_sale = 1
		} else {
			is_on_sale = 0
		}
		wx.showLoading({
			title: '玩命加载中',
		})
		if (switch_p) {
			switch_p = false
			page = page + 1;
			_this.ajaxList(page, _this.uid, is_follow, is_on_sale, _this.data.cur_key, _this.data.cur_keywords, function(data) {
				var temp = _this.data.indexList.concat(data)
				_this.setData({
					indexList: temp
				})
				wx.hideLoading();
				switch_p = true
			})
		}
	},
	handleCloseTap: function() {
		this.setData({
			followTk: false, //取消关注
			follow_id: '',
			canvasTk: false, //合成图片
			repTk: false, //举报
			rep_id: '',
			height: 0,
			tips: false,
			curTk_id:''
		})
	},
	videoPlay: function(e) { //视频播放
		var id = e.currentTarget.dataset.id
		this.setData({
			curr_id: id,
			vpause: false,
		})
		this.videoContext = wx.createVideoContext('myVideo' + id)
		this.videoContext.requestFullScreen({
			direction: 0
		});
		this.videoContext.play();
	},
	tapPause:function(){
		console.log(111111)
		this.videoContext.pause();
	},
	videoPause: function() { //监听视频暂停
		if (this.data.vpause) {
			this.setData({
				curr_id: '',
			})
		}
	},
	vwait: function() { //判断视频是否加载完成
		this.setData({
			vpause: true,
		})
	},
	handleShareTap: function(e) { //分享弹窗		
		var id = e.currentTarget.dataset.id;
		var nowToggle = this.data.indexList[id].toggle;
		//console.log(nowToggle)
		this.setData({
			curTk_id: id,
			['indexList[' + (id) + '].toggle']: !nowToggle
		})

	},
	handleFollowTap: function(e) { //点击关注
		var _this = this
		if (this.uid == '') {
			this.login.showIng()
			return false
		}
		var index = e.currentTarget.dataset.index;
		var id = this.data.indexList[index].store.id;
		var follow = this.data.indexList[index].store.is_follow;
		if (follow == 1) {
			this.setData({
				follow_id: id,
				followTk: true,
				follow_index: index
			})
		} else {
			Axios.post(app.globalData.baseUrl + 'App/Store/Store/store_follow', {
				store_id: id,
				uid: this.uid,
				sign: this.Md5,
				time: this.timesp
			}, (res) => {
				console.log(res)
				if (res.status == 1) {
					_this.data.indexList.forEach(function(item, key) {
						if (item.store.id == id) {
							_this.setData({
								['indexList[' + (key) + '].store.is_follow']: 1
							})
						}
					})
				}
			})
		}
	},
	handleCancelFoTap: function(e) { //取消关注
		var _this = this
		var index = e.currentTarget.dataset.index;
		var id = this.data.indexList[index].store.id;
		var follow = this.data.indexList[index].store.is_follow;
		if (follow == 1) {
			Axios.post(app.globalData.baseUrl + 'App/Store/Store/store_follow', {
				store_id: id,
				uid: this.uid,
				sign: this.Md5,
				time: this.timesp
			}, (res) => {
				console.log(res)
				if (res.status == 1) {
					_this.data.indexList.forEach(function(item, key) {
						if (item.store.id == id) {
							_this.setData({
								['indexList[' + (key) + '].store.is_follow']: 0,
								followTk: false
							})
						}
					})
				}
			})
		}
	},
	handleRepTap: function(e) { //举报
		if (this.uid == '') {
			this.login.showIng()
			return false
		}
		var id = e.currentTarget.dataset.id;
		var index = e.currentTarget.dataset.index;
		this.setData({
			rep_id: id,
			repTk: true,
			rep_index: index
		})
	},
	handleRepTkTap: function(e) { //提交举报
		var _this = this;
		var id = e.currentTarget.dataset.id;
		var index = e.currentTarget.dataset.index;
		if (this.data.repTkCon != '') {
			Axios.post(app.globalData.baseUrl + 'App/Store/Goods/goods_report', {
				uid: this.uid,
				goods_id: id,
				remark: _this.data.repTkCon,
				time: _this.timesp,
				sign: _this.Md5
			}, (res) => {
				console.log(res)
				if (res.status == 1) {
					var data = res.info
					_this.setData({
						tips: true,
						['indexList[' + (index) + '].is_report']: 1
					})
				} else {
					_this.showModal(res)
				}
			})
		} else {
			wx.showToast({
				title: '内容不能为空',
				image: '/img/warn-icon.png',
				duration: 1000,
				mask: true
			})
		}
	},
	bindAreaBlur: function(e) { //焦点
		var rep = e.detail.value
		this.setData({
			repTkCon: rep
		})
	},
	handleCanvasTap: function(e) { //合成图片				
		var index = e.currentTarget.dataset.index;
		var id = e.currentTarget.dataset.id;
		var HEIGHT = 670;
		var _this = this;
		var picinit = 0;
		var logoinit = 0;
		var textH = canvas.heightComputed(this.data.indexList[index].share_content, 510, 28, 37)
		picinit = textH
		textH += canvas.heightComputed(this.data.indexList[index].title, 560, 28, 37)
		logoinit = textH
		var fontWith = canvas.widthComputed(_this.data.nickName, 48)
		this.setData({
			height: (textH + HEIGHT),
			imgHeight: (textH + HEIGHT),
			top: e.detail.y
		})
		wx.showLoading({
			title: '绘图中···',
		})
		var avatarUrl = ''; //头像
		var temp_picture = ''; //产品图片
		var wxEwm = ''; //二维码
		Axios.post(app.globalData.baseUrl + 'App/PublicCommon/Share/share_img', {
			object_id: id,
			type: 2
		}, (res) => {
			console.log(res)
			if (res.status == 1) {
				var data = res.info
				wx.downloadFile({
					url: app.globalData.baseUrl + data,
					success: function(res) {
						wxEwm = res.tempFilePath
						wx.downloadFile({
							url: _this.data.avatarUrl,
							success: function(res) {
								avatarUrl = res.tempFilePath
								if (_this.data.indexList[index].master_picture.length > 0) {
									wx.downloadFile({
										url: app.globalData.baseUrl + _this.data.indexList[index].master_picture[0],
										success: function(res) {
											temp_picture = res.tempFilePath
											console.log(333)
											draws()
										},
										fail: function(fres) {
											wx.hideLoading();
										}
									})
								} else {
									draws()
								}
							},
							fail: function(fres) {
								wx.hideLoading();
							}
						})
					},
					fail: function(fres) {
						wx.hideLoading();
					},
				})
			} else {
				wx.hideLoading();
				_this.showModal(res)
			}
		})

		function draws() {
			app.ctx.setFillStyle('white')
			app.ctx.fillRect(0, 0, 650, (textH + HEIGHT) * .552)
			var avatar = {
				src: avatarUrl,
				x: 32,
				y: 53,
				width: 88,
				height: 88
			}
			canvas.drawAvatar(avatar)
			var proImg = {
				src: temp_picture,
				x: 178,
				y: 118 + picinit,
				width: 285,
				height: 285
			}
			canvas.drawImg(proImg)
			var canvasLogo = {
				src: '/img/canvasLogo.png',
				x: 43,
				y: 498 + logoinit,
				width: 333,
				height: 39
			}
			canvas.drawImg(canvasLogo)

			var author = {
				x: 139,
				y: 70,
				color: '#006e88',
				size: 28,
				align: 'left',
				baseline: 'middle',
				text: _this.data.nickName,
				bold: true
			}
			canvas.drawText(author)
			var sign = {
				x: 158 + fontWith,
				y: 70,
				color: '#f08415',
				size: 28,
				align: 'left',
				baseline: 'middle',
				text: '推荐',
				bold: true
			}
			canvas.drawText(sign)
			var adverter = {
				x: 139,
				y: 118,
				width: 510,
				height: 34,
				line: 2,
				color: '#3f4446',
				size: 28,
				align: 'left',
				baseline: 'middle',
				bold: false,
				text: _this.data.indexList[index].share_content
			}
			canvas.textWrap(adverter)
			var desc = {
				x: 40,
				y: 455 + picinit,
				width: 560,
				height: 34,
				line: 2,
				color: '#3f4446',
				size: 28,
				align: 'left',
				baseline: 'middle',
				bold: false,
				text: _this.data.indexList[index].title
			}
			canvas.textWrap(desc)
			var wxLogo = {
				src: wxEwm,
				x: 468,
				y: 455 + logoinit,
				width: 138,
				height: 138
			}
			var avatar2 = {
				src: avatarUrl,
				x: 506,
				y: 490 + logoinit,
				width: 68,
				height: 68
			}
			canvas.drawImg(wxLogo)
			canvas.drawAvatar(avatar2)
			app.ctx.draw()
			setTimeout(function() {
				wx.canvasToTempFilePath({
					x: 0,
					y: 0,
					canvasId: 'firstCanvas',
					success: function(res) {
						wx.hideLoading();
						// 设置画布高度						
						_this.setData({
							hcImg: res.tempFilePath,
							canvasTk: true, //合成图片
						})
					}
				});
			}, 1000)
		}
	},
	handleSaveTap: function() { //保存到相册
		console.log('保存到相册')
		wx.getImageInfo({
			src: this.data.hcImg, //
			success: function(res) {
				wx.saveImageToPhotosAlbum({
					filePath: res.path,
					success: function(dres) {
						wx.showToast({
							title: '已保存到相册'
						})
					}
				})
			}
		})
	},
	onShareAppMessage: function(e) {
		var id = e.target.dataset.id
		var index = e.target.dataset.index
		let _this = this;
		this.setData({
			shareTk: false,
		})
		return {
			title: _this.data.indexList[index].share_content, // 转发后 所显示的title
			path: '/pages/goodDetail/goodDetail?id=' + id, // 相对的路径
			success: function(res) { // 成功后要做的事情
				console.log(res.shareTickets[0])
				// console.log
				wx.getShareInfo({
					shareTicket: res.shareTickets[0],
					success: function(res) {

					},
					fail: function(res) {
						console.log(res)
					},
					complete: function(res) {
						console.log(res)
					}
				})
			},
			fail: function(res) {
				// 分享失败
				console.log(res)
			}
		}
	},
	handleMyfollowTap: function(e) { //只看关注
		if (this.uid == '') {
			this.login.showIng()
			return false
		}
		page = 1
		var id = e.currentTarget.dataset.id;
		var _this = this;
		this.setData({
			myfollow: !this.data.myfollow
		})
		var is_follow = 0;
		if (this.data.myfollow == true) {
			is_follow = 1
		} else {
			is_follow = 0
		}
		var is_on_sale = 0;
		if (this.data.saleing == true) {
			is_on_sale = 1
		} else {
			is_on_sale = 0
		}
		_this.load.showIng()
		_this.ajaxList(1, _this.uid, is_follow, is_on_sale, _this.data.cur_key, _this.data.cur_keywords, function(data) {
			_this.setData({
				indexList: data
			})
			_this.load.loadIng()
		})
	},
	handleSalingTap: function() { //只看在售
		page = 1
		this.setData({
			saleing: !this.data.saleing
		})
		var _this = this;
		var is_follow = 0;
		if (this.data.myfollow == true) {
			is_follow = 1
		} else {
			is_follow = 0
		}
		var is_on_sale = 0;
		if (this.data.saleing == true) {
			is_on_sale = 1
		} else {
			is_on_sale = 0
		}
		_this.load.showIng()
		_this.ajaxList(1, _this.uid, is_follow, is_on_sale, _this.data.cur_key, _this.data.cur_keywords, function(data) {
			_this.setData({
				indexList: data
			})
			_this.load.loadIng()
		})

	},
	handleAllTap: function() {
		var _this = this;
		var is_follow = 0;
		if (this.data.myfollow == true) {
			is_follow = 1
		} else {
			is_follow = 0
		}
		var is_on_sale = 0;
		if (this.data.saleing == true) {
			is_on_sale = 1
		} else {
			is_on_sale = 0
		}
		_this.setData({
			cur_key: ''
		})
		_this.load.showIng()
		_this.ajaxList(1, _this.uid, is_follow, is_on_sale, '', _this.data.cur_keywords, function(data) {
			_this.setData({
				indexList: data
			})
			_this.load.loadIng()
		})
	},
	handleSignTap: function(e) { //点击分类
		page = 1
		var id = e.currentTarget.dataset.id;
		var _this = this;
		var is_follow = 0;
		if (this.data.myfollow == true) {
			is_follow = 1
		} else {
			is_follow = 0
		}
		var is_on_sale = 0;
		if (this.data.saleing == true) {
			is_on_sale = 1
		} else {
			is_on_sale = 0
		}
		_this.load.showIng()
		_this.ajaxList(1, _this.uid, is_follow, is_on_sale, id, _this.data.cur_keywords, function(data) {
			_this.setData({
				indexList: data,
				cur_key: id
			})
			_this.load.loadIng()
		})
	},
	handleMoreTap: function() { //点击更多分类
		this.setData({
			signCls: !this.data.signCls
		})
	},
	keywords: function(e) {
		this.setData({
			cur_keywords: ''
		})
	},
	submit: function(e) {
		page = 1
		var _this = this
		var search_title = e.detail.value.search_title
		var is_follow = 0;
		if (this.data.myfollow == true) {
			is_follow = 1
		} else {
			is_follow = 0
		}
		var is_on_sale = 0;
		if (this.data.saleing == true) {
			is_on_sale = 1
		} else {
			is_on_sale = 0
		}
		_this.ajaxList(1, _this.uid, is_follow, is_on_sale, _this.data.cur_key, search_title, function(data) {
			_this.setData({
				indexList: data,
				firstKey: 1,
				cur_keywords: search_title
			})
		})

	},
	ajaxList: function(page, uid, is_follow, is_on_sale, search_hotwords_id, search_title, callback) {
		Axios.post(app.globalData.baseUrl + 'App/PublicCommon/Goods/search_goods?p=' + page, {
			uid: uid,
			is_follow: is_follow,
			is_on_sale: is_on_sale,
			search_hotwords_id: search_hotwords_id,
			search_title: search_title
		}, (res) => {
			console.log(res)
			if (res.status == 1) {
				var data = res.info
				callback(data)
			}
		})
	},
	previewImage: function (e) {
	  var current = e.currentTarget.dataset.src;
		var temp = [];	
		var index = e.currentTarget.dataset.index
		this.data.indexList[index].master_picture.forEach(function(item,key){
			temp.push(app.globalData.baseUrl + item)
		})						
		this.setData({
			showImg: temp
		})
	  wx.previewImage({
	    current: current, // 当前显示图片的http链接  
	    urls: this.data.showImg // 需要预览的图片http链接列表  
	  })
	} 
})
